var skinmap = {
	"1":"http://img03.taobaocdn.com/imgextra/i3/137009487/T2voFIXa8aXXXXXXXX_!!137009487.jpg",
	"2":"http://img03.taobaocdn.com/imgextra/i3/137009487/T26EFIXnJXXXXXXXXX_!!137009487.jpg",
	"3":"http://img03.taobaocdn.com/imgextra/i3/137009487/T2i54lXnFMXXXXXXXX_!!137009487.jpg",
	"4":"http://img02.taobaocdn.com/imgextra/i2/137009487/T2754lXk0MXXXXXXXX_!!137009487.jpg",
	"5":"http://img02.taobaocdn.com/imgextra/i2/137009487/T2JoFIXX0aXXXXXXXX_!!137009487.jpg",
	"6":"http://img04.taobaocdn.com/imgextra/i4/137009487/T2UoBIXdVaXXXXXXXX_!!137009487.jpg",
	"7":"http://img02.taobaocdn.com/imgextra/i2/137009487/T2ozXlXl4MXXXXXXXX_!!137009487.jpg"
};
function load_gridtable_temp(rule_type) {
  $("#rule_item_table").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 780,
    colNames: ["编号", "温度范围", "标题", "一行显示", "使用皮肤","当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      align:"center",
      sortable: false
    },
    {
      name: "TEMP_RANGE",
      index: "TEMP_RANGE",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formateTemp
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateRuleDelLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    loadComplete: function () {
      var result  =$("#rule_item_table").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#rule_table_pager",
    caption: "按温度显示"
  });
}

function load_gridtable_area(rule_type) {
  $("#rule_item_table").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 780,
    colNames: ["编号", "地区", "标题", "一行显示", "使用皮肤","当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      align:"center",
      sortable: false
    },
    {
      name: "AREA_NAMES",
      index: "AREA_NAMES",
      width: "200"
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200",
      sortable: false
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateRuleDelLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    loadComplete: function () {
      var result  =$("#rule_item_table").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"      
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#rule_table_pager",
    caption: "按地区显示"
  });
}


function load_gridtable_datetime(rule_type) {
  $("#rule_item_table").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 780,
    colNames: ["编号", "标题", "生效时间", "失效时间", "一行显示", "使用皮肤", "当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      align:"center",
      sortable: false
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "START_TIME",
      index: "START_TIME",
      width: "200"
    },
    {
      name: "END_TIME",
      index: "END_TIME",
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "120",
      align:"center",
      sortable: false,
      formatter: formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateRuleDelLink,
      width: "70"
    }
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    loadComplete: function () {
      var result  =$("#rule_item_table").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"      
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#rule_table_pager",
    caption: "按日期显示"
  });
}

function load_gridtable_time(rule_type) {
  $("#rule_item_table").jqGrid({
    url: "user_rule_sub.action?ruleType="+rule_type,
    datatype: "json",
    height: 250,
    width: 780,
    colNames: ["编号", "起止时间段", "标题", "一行显示", "使用皮肤", "当前状态","删除"],
    colModel: [{
      name: "RULE_ID",
      index: "RULE_ID",
      align:"center",
      sortable: false
    },
    {
      name: "TIME_RANGE",
      index: "TIME_RANGE",
      width: "160",
      align:"center",
      sortable: false,
      formatter:formateTime
    },
    {
      name: "TITLE",
      index: "TITLE",
      align:"center",      
      width: "200"
    },
    {
      name: "NUM_PER_TR",
      index: "NUM_PER_TR",
      width: "100",
      align:"center",      
      formatter:formateNumPerTr
    },
    {
      name: "SKIN_ID",
      index: "SKIN_ID",
      width: "120",
      align:"center",
      sortable: false,
      formatter:formatSkinImg
    },
    {
      name: "VALID_FLAG",
      index: "VALID_FLAG",
      align:"center",       
      width: "80"
    },   
    {
      name: "XX_RULE_ID",
      index: "XX_RULE_ID",
      formatter:formateRuleDelLink,
      width: "70"
    } 
    ],
    viewrecords: true,
    rowNum: 5,
    rowList: [5, 10, 15],
    loadComplete: function () {
      var result  =$("#rule_item_table").getGridParam('userData');
      if(validate_response_result(result)){
        setImageCellHeight();
      }
    },
    onSelectRow: function (rowid, status) {
    },
    onSelectAll: function (ids, status) {
    },
    jsonReader: {
      root: "userRuleSubs",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false,
      userdata: "result"      
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#rule_table_pager",
    caption: "按时间段显示"
  });
}

function setImageCellHeight() { 
    var ids = $("#rule_item_table").getDataIDs(); 
    for (var i = 0; i < ids.length; i++) { 
      var cl = ids[i]; 
      $("#rule_item_table").setRowData(ids[i],false, {height: 40}); 
    } 
}

function deleteUserRuleSubByButton(){
  var rowids=$("#rule_item_table").jqGrid('getGridParam','selarrrow');
  if(rowids==''){
    alert("请在下表中选择要删除的规则");
    return;
  }
  
  rowidArray = (''+rowids).split(",");
  
  var rule_ids='';
  for(var i = 0;i<rowidArray.length;i++){
    var row_data = $("#rule_item_table").jqGrid("getRowData", rowidArray[i]);
    rule_ids+=row_data.RULE_ID+',';
  }
  rule_ids = rule_ids.slice(0,rule_ids.length-1);
  deleteUserRuleSub(rule_ids);
}

function deleteUserRuleSubByLink(rule_id){
  deleteUserRuleSub(rule_id);
}

function deleteUserRuleSub(rule_ids){
  var params={
    ruleIds:rule_ids,
    ruleType:$("input[name='rule_type']").val()
  }
  $.post("delete_rule_sub.action",params,delete_rule_sub_response,"json");
}

function delete_rule_sub_response(data){
  if(validate_response_result(data.result)){
    $("#rule_item_table").trigger("reloadGrid");
  }
}

function validate_response_result(result){
  if(Number(result.code<0)){
    alert(result.msg);
    return false;
  }
  return true;;
}

function formateRuleDelLink(cellvalue, options, rowObject) {
   var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>删除</span>"
   return "<a href='javascript:deleteUserRuleSubByLink("+rowObject.RULE_ID+");'>" + title + "</a>";
}
function formateTemp(cellvalue, options, rowObject) {
   return rowObject.START_TEMP+"℃~"+rowObject.END_TEMP+"℃";
}

function formateTime(cellvalue, options, rowObject) {
   return rowObject.S_TIME+"~"+rowObject.E_TIME;
}

function formateNumPerTr(cellvalue, options, rowObject) {
   return cellvalue+"个宝贝";
}
function formatSkinImg(cellvalue, options, rowObject) {
	return "<img src='"+skinmap[cellvalue]+"' height='25' width='80'></img>";
}

function validate_temp() {
	var s1 = $("input[name='start_temp']").val();
	var s2 = $("input[name='end_temp']").val();
	var start_temp = new Number(s1);
	var end_temp = new Number(s2);
	
	if(start_temp>=end_temp) {
		alert("最低温度必须小于最高温度");
		return false;
	}
	
	return true;
}

function validate_area() {
	var nonchk = true;
	$("input[name='area']:checked").each(function() {
		nonchk = false;
	});
	
	if(nonchk) {
		alert("您尚未选择地区！！！");
		return false;
	}
	
	return true;
}

function validate_date() {
	var s1 = $("input[name='effect_date']").val();
	var s2 = $("input[name='expire_date']").val();
	if(empty(s1)) {
		alert("您尚未设置生效时间！！！");
		$("input[name='effect_date']").focus();
		return false;
	}
	if(empty(s2)) {
		alert("您尚未设置失效时间！！！");
		$("input[name='expire_date']").focus();
		return false;
	}
	var effect_date =new Date(Date.parse(s1.replace(/-/g, "/")));
	var expire_date =new Date(Date.parse(s2.replace(/-/g, "/")));
	if(effect_date>=expire_date) {
		alert("失效时间不能小于生效时间！！！");
		return false;
	}
	
	return true;
}

function validate_time() {
	var s1 = $("input[name='start_time']").val();
	var s2 = $("input[name='end_time']").val();
	var start_time = Date.parse("2011/01/01 "+s1);
	var end_time = Date.parse("2011/01/01 "+s2);
	if(start_time>=end_time) {
		alert("结束时间不能小于起始时间！！！");
		return false;
	}
	return true;
}

